﻿using UnityEngine;
using Entitas;
using UnityEngine.UI;
using System.Linq;
class WeaponListView : MonoBehaviour, IEquippedWeaponListener
{
    PlayerEntity player;
    public void OnEquippedWeapon(PlayerEntity entity, IWeapon value)
    {
        var newpos = new Vector3();
        if (transform.GetChild(0).name == value.Name)
        {
            newpos = transform.GetChild(0).position;
            newpos.y -= 30f;
            transform.GetChild(2).position = newpos;
        }
        else
        {
            newpos = transform.GetChild(1).position;
            newpos.y -= 30f;
            transform.GetChild(2).position = newpos;
        }
    }

    private void Start()
    {
        if (Contexts.sharedInstance.player.count == 1)
        {
            player = Contexts.sharedInstance.player.GetEntities().SingleEntity<PlayerEntity>();
            System.Collections.Generic.List<string> weaponNames = new System.Collections.Generic.List<string>();
            foreach (var w in player.inventory.value.Where(k=>k.Key.isWeaponitem==true))
            {
                weaponNames.Add(w.Key.Name);
            }
         
            for (int i = 0; i < 2; i++)
            {
                Debug.Log(weaponNames[i]);
                transform.GetChild(i).GetComponent<Image>().sprite = Resources.Load<Sprite>(@"UI Sprites\" + weaponNames[i] + "_UI");
                transform.GetChild(i).GetComponent<Image>().name = weaponNames[i];
            }
            var initialpos = new Vector3();
            initialpos = transform.GetChild(0).position;
            initialpos.y -= 30f;
            player.AddEquippedWeaponListener(this);

            Color Visible = transform.GetChild(0).GetComponent<Image>().color;
            Visible.a = 255;
            transform.GetChild(0).GetComponent<Image>().color = Visible;
            transform.GetChild(1).GetComponent<Image>().color = Visible;
            transform.GetChild(2).GetComponent<Image>().color = Visible;
        }


    }
}

